/** @file    FormCommunicationManager.cpp
 *  @author  Alessandro Polo
 *  @version $Id: FormCommunicationManager.cpp 2748 2010-07-11 22:07:06Z alex $
 *  @brief
 * File containing methods for the wosh::gui::FormCommunicationManager class.
 * The header for this class can be found in FormCommunicationManager.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "FormCommunicationManager.h"
 #include "ui_FormCommunicationManager.h"

 #include <core/UserManager.h>
 #include <core/WoshKernel.h>
 #include <core/MethodsCommon.h>
 #include <core/Request.h>
 #include <core/Response.h>

 #include <framework/gui/widgets/wCommunication.h>

 #include <interfaces/services/CommunicationManager.h>

 #include <QtGui>
 #include <framework/gui/GuiUtilities.h>


using namespace std;
namespace wosh {
 namespace gui {

// WOSH_REGISTER(wosh::gui::FormCommunicationManager, "wosh::gui::IWidgetGeneric", 1.0, _static_FormCommunicationManager )
// WOSH_GUI_MAP(wosh::gui::FormCommunicationManager, "wosh::interfaces::services::CommunicationManager", 0.0, _static_Map_FormCommunicationManager )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

FormCommunicationManager::FormCommunicationManager(QWidget *parent) : IWidgetGeneric(parent)
 {
	this->m_ui = new Ui::FormCommunicationManager();
	this->m_ui->setupUi(this);

	this->setWindowTitle("Communication Manager");
	this->getWidgetURI().setName( Utilities::format("FormCommunicationManager#%10lu", Utilities::randomLong()) );
	this->getTargetURI().clearPathName();
	this->getTargetURI().setName("Communication");


	this->m_ui->tableViewComm->setMethodList(_CommunicationManager_METHOD_communicators);
	connect(this->m_ui->tableViewComm, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );
	this->m_ui->tableViewInter->setMethodList(_CommunicationManager_METHOD_interpreters);
	connect(this->m_ui->tableViewInter, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );
	this->m_ui->tableViewMessagesAr->setMethodList(_CommunicationManager_METHOD_messages_archived);
	connect(this->m_ui->tableViewMessagesAr, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );
	this->m_ui->tableViewMessagesPr->setMethodList(_CommunicationManager_METHOD_messages);
	connect(this->m_ui->tableViewMessagesPr, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );

	mapMethodButton(_CommunicationManager_METHOD_update_comm, this->m_ui->btnCommForceUpdate);
	mapMethodButton(_CommunicationManager_METHOD_update_inter, this->m_ui->btnInterForceUpdate);
	mapMethodButton(_CommunicationManager_METHOD_messages_dump, this->m_ui->btnDumpMessages);

	cmbSendMessageType_activated( "wosh::communication::TextMessage" );

	connect(this->m_ui->cmbSendMessageType, SIGNAL(activated(const QString&)), this, SLOT(cmbSendMessageType_activated(const QString&)));
	connect(this->m_ui->btnSendMessageSend, SIGNAL(clicked()), this, SLOT(btnSendMessageSend_clicked()));
	connect(this->m_ui->btnSendMessageReset, SIGNAL(clicked()), this, SLOT(btnSendMessageReset_clicked()));
	connect(this->m_ui->btnSendMessageOptions, SIGNAL(clicked()), this, SLOT(btnSendMessageOptions_clicked()));

 }

FormCommunicationManager::~FormCommunicationManager()
 {
	delete this->m_ui;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormCommunicationManager::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormCommunicationManager::refreshAll() {
	this->setWindowTitle( QString("Communication Manager [%1]").arg(this->getTargetURI().getKernelName().c_str()) );
	this->m_ui->tableViewComm->refreshData();
	this->m_ui->tableViewInter->refreshData();
	this->m_ui->tableViewMessagesAr->refreshData();
	this->m_ui->tableViewMessagesPr->refreshData();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormCommunicationManager::cmbSendMessageType_activated( const QString& value ) {
	Notification* notif = NULL;
	if ( value == "wosh::communication::TextMessage" ) {
		this->m_ui->stackedMessage->setCurrentIndex(1);
		notif = this->m_ui->wTextMessage->newObject();
	 }
	else if ( value == "wosh::communication::AudioMessage" ) {
		this->m_ui->stackedMessage->setCurrentIndex(2);
		notif = this->m_ui->wAudioMessage->newObject();
	 }
	if ( notif != NULL )
		notif->setSender_User("root");

	this->m_ui->btnSendMessageOptions->setChecked(false);
 }

void FormCommunicationManager::btnSendMessageSend_clicked() {
	Notification* notif = getCurrentNotification();
	postMessageForTarget( new Message(notif->clone()) );
 }

void FormCommunicationManager::btnSendMessageReset_clicked() {
	if ( this->m_ui->stackedMessage->currentIndex() == 0 )
		this->m_ui->wNotification->loadObject(NULL);
	else if ( this->m_ui->stackedMessage->currentIndex() == 1 )
		this->m_ui->wTextMessage->loadObject(NULL);
	else if ( this->m_ui->stackedMessage->currentIndex() == 2 )
		this->m_ui->wAudioMessage->loadObject(NULL);
 }

void FormCommunicationManager::btnSendMessageOptions_clicked() {
	if ( this->m_ui->btnSendMessageOptions->isChecked() ) {
		this->m_ui->wNotification->loadObject( getCurrentNotification() );
		this->m_ui->stackedMessage->setCurrentIndex(0);
	 }
	else {
		if ( this->m_ui->cmbSendMessageType->currentText() == "wosh::communication::TextMessage" )
			this->m_ui->stackedMessage->setCurrentIndex(1);
		else if ( this->m_ui->cmbSendMessageType->currentText() == "wosh::communication::AudioMessage" )
			this->m_ui->stackedMessage->setCurrentIndex(2);
	 }
//	this->m_ui->btnSendMessageOptions->setChecked( this->m_ui->stackedMessage->currentIndex() );
 }

Notification* FormCommunicationManager::getCurrentNotification(){
	if ( this->m_ui->cmbSendMessageType->currentText() == "wosh::communication::TextMessage" )
		return this->m_ui->wTextMessage->saveSerializable();

	else if ( this->m_ui->cmbSendMessageType->currentText() == "wosh::communication::AudioMessage" )
		return this->m_ui->wAudioMessage->saveSerializable();

	return NULL;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormCommunicationManager::busMessage( const Message& message, const Bus* ) {
	if ( !isMessageForMeOrFromTarget(message) )
		return;

	if ( message.getContent()->isResponse() ) {
		const wosh::Response* response = message.getContent()->asResponse();
		this->m_ui->tableViewComm->evalResponse(response);
		this->m_ui->tableViewInter->evalResponse(response);
		this->m_ui->tableViewMessagesAr->evalResponse(response);
		this->m_ui->tableViewMessagesPr->evalResponse(response);

		promptMethodResponse(message);
	 }
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
